using System;
using System.Collections.Generic;
using System.Text;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.Interfaces;
using SharpObjects.Shared;
using SharpObjects.Configuration;

namespace SharpObjects.Data
{
    /*
  <sql_statements>
    <statement id="insert_folder_tree">
      <sql>
        insert into folder_tree (id, parentid, childid, datemodified, modifiedby, datecreated, createdby )
        values ({0}, {1}, {2}, {3}, {4}, {5}, {6})
      </sql>
      <parameters>id, parentid, childid, datemodified, modifiedby, datecreated, createdby </parameters>
    </statement>
    <statement id="insert_items">
      <sql>
        insert into items (itemid, description )
        values ({0}, {1})
      </sql>
      <parameters>itemid, description </parameters>
    </statement>
  </sql_statements>
     
     */

    public class SqlStatement : IXmlInitializable
    {
        protected readonly char[] delimiters = new char[] { ',', ';' };
        protected readonly char[] white_space = new char[] { '\t', '\n', '\v', '\f', '\r' }; // 9, 10, 11, 12, 13

        private string id;
        private string sql;
        private string[] parameters;


        public string[] Parameters
        {
            get { return this.parameters; }
            set { this.parameters = value; }
        }


        public string Sql
        {
            get { return this.sql; }
            set { this.sql = value; }
        }



        #region IXmlInitializable Members

        
/*
    <statement id="insert_items">
      <sql>
        insert into items (itemid, description )
        values ({0}, {1})
      </sql>
      <parameters>itemid, description </parameters>
    </statement>
*/

        public void Initialize(System.Xml.XmlElement initializationElement)
        {
            XmlConfigurationProvider config = new XmlConfigurationProvider(initializationElement);

            this.id = config.GetString("@id");
            string sql = config.GetString("sql");

            for (int k = 0; k < white_space.Length; k++)
            {
                sql = sql.Replace(white_space[k], ' ');
            }

            this.sql = sql.Trim();
            string parameters = config.GetString("parameters");
            this.parameters = parameters.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);

            for (int k = 0; k < this.parameters.Length; k++)
            {
                this.parameters[k] = this.parameters[k].Trim();
            }

            this.IsInitialized = true;
        }

        public bool IsInitialized { get; set; }

        public string Id
        {
            get
            {
                return this.id;
            }
            set
            {
            }
        }

        #endregion
    }
}
